Comparación entre dos emisoras


In [2]:
# Imports
%matplotlib inline
import pandas as pd
import pandas.io.data
import numpy as np
import pytz
from datetime import datetime
import zipline as zp
import matplotlib.pyplot as plt
import matplotlib as mpl

# Emisoras para comparar
emisora = 'AZTECACPO.MX'
emisora_2 = 'TLEVISACPO.MX'

start = datetime(2010, 5, 1, 0, 0, 0, 0, pytz.utc)
data_emisora = pd.io.data.get_data_yahoo(emisora, start=start)
data_emisora_2 = pd.io.data.get_data_yahoo(emisora_2, start=start)

# Indicadores

print str(emisora), data_emisora.describe()
print str(emisora_2), data_emisora_2.describe()

# Calcula cambios entre días
data = pd.DataFrame()
data[str(emisora)] = data_emisora['Adj Close'].pct_change()
data[str(data_emisora_2)] = data_emisora_2['Adj Close'].pct_change()

data.columns = [emisora, emisora_2]

print data.head()


AZTECACPO.MX               Open         High          Low        Close        Volume  \
count  1072.000000  1072.000000  1072.000000  1072.000000  1.072000e+03   
mean      7.984767     8.112500     7.847509     7.980718  2.609058e+06   
std       0.847380     0.865171     0.840724     0.853238  4.858775e+06   
min       5.450000     5.620000     5.450000     5.560000  0.000000e+00   
25%       7.350000     7.490000     7.200000     7.350000  8.033750e+05   
50%       8.150000     8.290000     8.010000     8.150000  2.290600e+06   
75%       8.680000     8.820000     8.530000     8.670000  3.602500e+06   
max       9.760000     9.950000     9.560000     9.850000  1.407378e+08   

         Adj Close  
count  1072.000000  
mean      7.980718  
std       0.853238  
min       5.560000  
25%       7.350000  
50%       8.150000  
75%       8.670000  
max       9.850000  
TLEVISACPO.MX               Open         High          Low        Close           Volume  \
count  1099.000000  1099.000000  1099.000000  1099.000000      1099.000000   
mean     63.496388    64.190546    62.860864    63.548944   3737457.415833   
std      12.464754    12.618150    12.398963    12.505809   2835244.698827   
min      45.190000    45.670000    44.800000    45.190000         0.000000   
25%      54.255000    54.895000    53.660000    54.325000   2236850.000000   
50%      60.100000    60.840000    59.710000    60.230000   3199100.000000   
75%      71.225000    71.875000    70.400000    71.105000   4521250.000000   
max      98.400000    99.210000    97.250000    97.870000  38209600.000000   

         Adj Close  
count  1099.000000  
mean     63.093348  
std      12.788920  
min      44.370000  
25%      53.610000  
50%      59.850000  
75%      70.870000  
max      97.870000  
            AZTECACPO.MX  TLEVISACPO.MX
Date                                   
2010-05-03           NaN            NaN
2010-05-04     -0.029197      -0.036697
2010-05-05      0.016541      -0.012836
2010-05-06     -0.010355      -0.021812
2010-05-07     -0.004484      -0.008576

In [9]:
data.plot()
plt.ylabel('% Rendimiento');
plt.figure()


Out[9]:
<matplotlib.figure.Figure at 0xaecc3c8c>
<matplotlib.figure.Figure at 0xaecc3c8c>

In [10]:
dias_promedio = 30

data = pd.DataFrame()
mavg = pd.ewma(data_emisora['Adj Close'], dias_promedio)
mavg_2 = pd.ewma(data_emisora_2['Adj Close'], dias_promedio)

data[str(emisora)] = mavg
data[str(emisora_2)] = mavg_2

data.plot()
plt.figure()


Out[10]:
<matplotlib.figure.Figure at 0xaef4bb6c>
<matplotlib.figure.Figure at 0xaef4bb6c>

RSI


In [11]:
delta = data_emisora['Close'].diff()
dUp, dDown = delta.copy( ), delta.copy( )
dUp[ dUp < 0 ] = 0
dDown[ dDown > 0 ] = 0

n=14
RolUp = pd.ewma(dUp, n)
RolDown = pd.ewma(dDown, n).abs()

RS = RolUp / RolDown
RSI = 100. - 100./(1.+RS)
RSI.plot();

plt.axhline(20, color='k', alpha=0.2)
plt.annotate('sobreventa',xy=(0.5, 0.3), xycoords='figure fraction', fontsize=20, alpha=0.4, ha='center')
plt.axhline(80, color='k', alpha=0.2)
plt.annotate('sobrecompra',xy=(0.5, 0.8), xycoords='figure fraction', fontsize=20, alpha=0.4,ha='center')
plt.title('RSI %s (%i dias)' % (emisora, n));
plt.ylim([0,100]);
plt.ylabel('%');
plt.figure()


Out[11]:
<matplotlib.figure.Figure at 0xaebd136c>
<matplotlib.figure.Figure at 0xaebd136c>

Riesgo vs Rendimientos


In [14]:
df = pd.io.data.get_data_yahoo(['AZTECACPO.MX', 'TLEVISACPO.MX', '^MXX'], 
                               start=datetime(2010, 1, 1))['Adj Close']

rets = df.pct_change()

fig=plt.figure(figsize=(6,6))
plt.scatter(rets.mean(), rets.std(), s=50)
plt.xlabel('Expected returns')
plt.ylabel('Risk')
for label, x, y in zip(rets.columns, rets.mean(), rets.std()):
    plt.annotate(
        label, 
        xy = (x, y), xytext = (20, -20),
        textcoords = 'offset points', ha = 'right', va = 'bottom',
        bbox = dict(boxstyle = 'round,pad=0.5', fc = 'w', alpha = 0.5),
        arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))



In [ ]: